/**
 * 
 */
package com.vinhvt.effiport.financial.ep;

import java.util.*;


import com.vinhvt.effiport.algorithm.genetic.GAsParameters;
import com.vinhvt.effiport.util.Helper;

/**
 * The Class Asset.
 * 
 * @author Vinnie
 */
public class Asset {
	private Underlying underlying;	
	private double weight = 0.0;
	private double profit = 0.0;			
	
	/**
	 * Instantiates a new asset.
	 * 
	 * @param symbol
	 *            the symbol of asset
	 */
	public Asset(Underlying underlying) {
		this.setUnderlying(underlying);
	}
			
	/**
	 * Calculating Average Return.
	 * 
	 * @return the double
	 */
	public final double calcAverageReturn() {
		double result = 0;

		int times = this.getRateOfReturnList().size();		
		for (int i = 0; i < times; i++) {
			result += this.getRateOfReturnList().get(i).doubleValue();			
		}
		result /= (times);
		//result = Helper.round(result);

		return result;
	}
	

	/**
	 * Load Volatilization list.
	 * 
	 * @return the list< double>
	 */
	public final List<Double> loadVolatilizationList() {
		List<Double> result = new ArrayList<Double>();

		List<Double> rateOfReturnList = this.getUnderlying().getRateOfReturnList();
		double expectedReturn = this.calcAverageReturn();

		int times = rateOfReturnList.size();
		for (int i = 0; i < times; i++) {
			double value = rateOfReturnList.get(i) - expectedReturn;
			result.add(value);
		}

		return result;
	}

	/**
	 * Calculating variance.
	 * 
	 * @return the double
	 */
	public double calcVariance() {
		return calcCovarWith(this);
	}

	/**
	 * Calc covar with.
	 * 
	 * @param other
	 *            the other
	 * 
	 * @return the double
	 */
	public final double calcCovarWith(Asset other) {
		double result = 0.0;

		List<Double> expectedReturnList1 = this.loadVolatilizationList();
		List<Double> expectedReturnList2 = other.loadVolatilizationList();

		int times = expectedReturnList1.size();
		for (int i = 0; i < times; i++) {
			double value = expectedReturnList1.get(i)
					* expectedReturnList2.get(i);
			result += value;
		}
		result /= times;

		return result;
	}

	@Override
	public String toString() {
		String result = "";
		result += "Ticker:" + this.getSymbol();
		for (int i = 0; i < this.underlying.getTransactions().size(); i++) {

		}
		return result;
	}

	/**
	 * Sets the weight.
	 * 
	 * @param weight
	 *            the new weight
	 */
	public void setWeight(double weight) {
		this.weight = weight;
	}

	/**
	 * Gets the weight.
	 * 
	 * @return the weight
	 */
	public double getWeight() {
		return weight;
	}		

	/**
	 * Gets the lst rate of return.
	 * 
	 * @return the lst rate of return
	 */
	public List<Double> getRateOfReturnList() {
		return this.getUnderlying().getRateOfReturnList();
	}
	
	public void setProfit(double profit) {
		this.profit = profit;
	}

	public double getProfit() {
		return profit;
	}

	public void setUnderlying(Underlying underlying) {
		this.underlying = underlying;
	}

	public Underlying getUnderlying() {
		return underlying;
	}
	
	public String getSymbol() {
		return this.underlying.getSymbol();
	}	

	public double getVariance() {
		return this.calcVariance();
	}	
}
